package com.mx.dao.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.print.DocFlavor.STRING;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.mx.dao.Isalary;
import com.mx.entity.Salary;
@Repository
public class SalaryImpl implements Isalary {
   //注入session
	@Autowired
	 private  SessionFactory  sessionfactory;
	@Override
	public List<Salary> getAllValue() {
		Session session=sessionfactory.openSession();
		Query query= session.createQuery("from Salary");
		@SuppressWarnings("unchecked")
		List<Salary> list= query.list();
		session.close();
		return list;
	}
	@SuppressWarnings({ "unchecked", "null" })
	@Test
   public List<Object> getSalaryByYear(){
	 //SELECT ym,id,`name`,salary FROM salary GROUP BY ym ,`name` ORDER BY id ASC
	    Session session=sessionfactory.openSession();
	    SQLQuery  querymonth= session.createSQLQuery("SELECT distinct(ym) FROM salary");
		SQLQuery  query= session.createSQLQuery("SELECT salary FROM salary WHERE name='张三'");
		SQLQuery  query1= session.createSQLQuery("SELECT salary FROM salary WHERE name='李四'");
		List listquery1=query.list();
		List listquery2= query1.list();
		Map<Object, Object> mapdata1 = new HashMap<Object, Object>();
		mapdata1.put("name", "张三");
		Map<Object, Object> mapdata2 = new HashMap<Object, Object>();
		mapdata2.put("name", "李四");
		BigDecimal  decimal=null;
		String [] str1 = new String[listquery1.size()];
		String [] str2 = new String[listquery2.size()];
		for (int i = 0; i < listquery1.size(); i++) {
		     decimal=(BigDecimal) listquery1.get(i);
              String aa=decimal.toString();
              
		     str1[i]=aa;
		}
		mapdata1.put("data",str1);
		
		 BigDecimal decimal1=null;
		for (int i = 0; i < listquery2.size(); i++) {
			decimal1=(BigDecimal) listquery2.get(i);
			str2[i]=decimal1.toString();
		}
		mapdata2.put("data", str2);
        List<Object> listdata=new ArrayList<Object>();
		
        List<String> listmonth= querymonth.list();
		listdata.add(listmonth);
        listdata.add(mapdata1);
		listdata.add(mapdata2);
		session.close();
		return listdata; 
		
		
	   
   };
}
